#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include "functions.h"

static int Im_W[5];
static int Im_H[5];
static int Pix_L[5];
static GLubyte *Pix_D[5];

void display_0(void)
{
	glClear(GL_COLOR_BUFFER_BIT);//绘制像素
	glDrawPixels(Im_W[0], Im_H[0],GL_BGR_EXT, GL_UNSIGNED_BYTE, Pix_D[0]);
	glutSwapBuffers();
}
void display_1(void)
{
	glClear(GL_COLOR_BUFFER_BIT);//绘制像素
	glDrawPixels(Im_W[1], Im_H[1],GL_BGR_EXT, GL_UNSIGNED_BYTE, Pix_D[1]);
	glutSwapBuffers();
}
void display_2(void)
{
	glClear(GL_COLOR_BUFFER_BIT);//绘制像素
	glDrawPixels(Im_W[2], Im_H[2],GL_BGR_EXT, GL_UNSIGNED_BYTE, Pix_D[2]);
	glutSwapBuffers();
}
void display_3(void)
{
	glClear(GL_COLOR_BUFFER_BIT);//绘制像素
	glDrawPixels(Im_W[3], Im_H[3],GL_BGR_EXT, GL_UNSIGNED_BYTE, Pix_D[3]);
	glutSwapBuffers();
}
void display_4(void)
{
	glClear(GL_COLOR_BUFFER_BIT);//绘制像素
	glDrawPixels(Im_W[4], Im_H[4],GL_BGR_EXT, GL_UNSIGNED_BYTE, Pix_D[4]);
	glutSwapBuffers();
}

int main(int argc, char* argv[])
{
	FILE *pFile;
	double *temp;//用于传递变换的复矩阵
	//******************************
	//矩孔傅里叶变换
	Im_W[0] = 600;
	Im_H[0] = 600;
	Pix_L[0] = Im_W[0] * 3;
    while( Pix_L[0] % 4 != 0 ) ++Pix_L[0];
    Pix_L[0] *= Im_H[0];

    Pix_D[0] = (GLubyte*)malloc(Pix_L[0]); if(Pix_D[0] == 0 ) exit(0);//分配像素矩阵
	temp = (double*)malloc(sizeof(double)*Im_W[0]*Im_H[0]*2); if(temp == 0 ) exit(0);//分配中间复矩阵
	init_Pix(Pix_D[0] , Im_W[0] , Im_H[0] , Pix_L[0] , 0,0,0);//像素矩阵初始化

	init_rec(temp , Im_W[0] , Im_H[0] , 20);//矩孔初始化
	FFT2(temp , Im_W[0] , Im_H[0]);//傅里叶变换
	fftshift(temp , Im_W[0] , Im_H[0]);//移动级次

	CP_m2i(temp , Pix_D[0] , Im_W[0] , Im_H[0] , Pix_L[0] , 1,1, 0,1,0, 1);//变换结果加至图像（取模，规范最大值225）
	free(temp);
	//******************************
    pFile = fopen("xx.bmp", "rb");//打开文件
    if( pFile == 0 ) exit(0);

    fseek(pFile, 0x0012, SEEK_SET);//读取图象的大小信息
    fread(&Im_W[1], sizeof(Im_W[1]), 1, pFile);
    fread(&Im_H[1], sizeof(Im_H[1]), 1, pFile);
    Pix_L[1] = Im_W[1] * 3;//计算像素数据长度
    while( Pix_L[1] % 4 != 0 ) ++Pix_L[1];
    Pix_L[1] *= Im_H[1];

    Pix_D[1] = (GLubyte*)malloc(Pix_L[1]);//读取像素数据
    if( Pix_D[1] == 0 ) exit(0);
    fseek(pFile, 54, SEEK_SET);
    fread(Pix_D[1], Pix_L[1], 1, pFile);
    fclose(pFile);//关闭文件

	temp = (double*)malloc(sizeof(double)*Im_W[1]*Im_H[1]*2);
    if(temp == 0 ) exit(0);

	CP_i2m(Pix_D[1] , temp , Im_W[1] , Im_H[1] , Pix_L[1] , 1,0,0, 1,0);
	trans_Pix(Pix_D[1] , Im_W[1] , Im_H[1] , Pix_L[1] , 0,1,1);//清除红色像素原始值

	screen_grid(temp,Im_W[1],Im_H[1],5,5,0.5,1, 5,5,0.5,1);
	screen_half(temp,Im_W[1],Im_H[1],100);

	FFT2(temp , Im_W[1] , Im_H[1]);

	//fftshift(temp , Im_W[1] , Im_H[1]);
	//screen_rec(temp, Im_W[1], Im_H[1], 800,800 , 0,0, 2, 2, 0,0);
	//fftshift(temp , Im_W[1] , Im_H[1]);

	//iFFT2(temp , Im_W[1] , Im_H[1]);

	CP_m2i(temp , Pix_D[1] , Im_W[1] , Im_H[1] , Pix_L[1] , 1,1, 1/10.0,0,0, 1);//变换结果加至图像（取模，规范最大值225）


	//CP_i2m(Pix_D[1] , temp , Im_W[1] , Im_H[1] , Pix_L[1] , 0,1,0, 1,0);
	trans_Pix(Pix_D[1] , Im_W[1] , Im_H[1] , Pix_L[1] , 1,0,1);//清除绿色像素原始值
	//FFT2(temp , Im_W[1] , Im_H[1]);

	//fftshift(temp , Im_W[1] , Im_H[1]);
	//screen_rec(temp, Im_W[1], Im_H[1], 800,800 , 0,0, 2, 2, 0,0);
	//fftshift(temp , Im_W[1] , Im_H[1]);

	//iFFT2(temp , Im_W[1] , Im_H[1]);
	//fftshift(temp , Im_W[1] , Im_H[1]);
	//CP_m2i(temp , Pix_D[1] , Im_W[1] , Im_H[1] , Pix_L[1] , 1,1, 0,1,0, 1);//变换结果加至图像（取模，规范最大值225）


	//CP_i2m(Pix_D[1] , temp , Im_W[1] , Im_H[1] , Pix_L[1] , 0,0,1, 1,0);
	trans_Pix(Pix_D[1] , Im_W[1] , Im_H[1] , Pix_L[1] , 1,1,0);//清除蓝色像素初始值
	//FFT2(temp , Im_W[1] , Im_H[1]);

	//fftshift(temp , Im_W[1] , Im_H[1]);
	//screen_rec(temp, Im_W[1], Im_H[1], 800,800 , 0,0, 2, 2, 0,0);
	//fftshift(temp , Im_W[1] , Im_H[1]);

	//iFFT2(temp , Im_W[1] , Im_H[1]);
	//CP_m2i(temp , Pix_D[1] , Im_W[1] , Im_H[1] , Pix_L[1] , 1,1, 0,0,1, 1);//变换结果加至图像（取模，规范最大值225）

	free(temp);
	//******************************/
	//圆孔傅里叶变换
	Im_W[2] = 700;
	Im_H[2] = 700;
	Pix_L[2] = Im_W[2] * 3;
    while( Pix_L[2] % 4 != 0 ) ++Pix_L[2];
    Pix_L[2] *= Im_H[2];

    Pix_D[2] = (GLubyte*)malloc(Pix_L[2]); if(Pix_D[2] == 0 ) exit(0);
	temp = (double*)malloc(sizeof(double)*Im_W[2]*Im_H[2]*2); if(temp == 0 ) exit(0);
	init_Pix(Pix_D[2] , Im_W[2] , Im_H[2] , Pix_L[2] , 0,0,0);//像素矩阵初始化

	init_circ(temp , Im_W[2] , Im_H[2] , 16);//圆孔初始化
	FFT2(temp , Im_W[2] , Im_H[2]);//傅里叶变换
	fftshift(temp , Im_W[2] , Im_H[2]);//移动级次

	CP_m2i(temp , Pix_D[2] , Im_W[2] , Im_H[2] , Pix_L[2] , 1,1, 0,1,0, 1);//变换结果加至图像（取模，规范最大值225）
	free(temp);
	//*****************************
	Im_W[3] = 600;
	Im_H[3] = 600;
	Pix_L[3] = Im_W[3] * 3;
    while( Pix_L[3] % 4 != 0 ) ++Pix_L[3];
    Pix_L[3] *= Im_H[3];

    Pix_D[3] = (GLubyte*)malloc(Pix_L[3]); if(Pix_D[3] == 0 ) exit(0);//分配像素矩阵
	temp = (double*)malloc(sizeof(double)*Im_W[3]*Im_H[3]*2); if(temp == 0 ) exit(0);//分配中间复矩阵
	init_Pix(Pix_D[3] , Im_W[3] , Im_H[3] , Pix_L[3] , 0,255,0);//像素矩阵初始化


	CP_i2m(Pix_D[3] , temp , Im_W[3] , Im_H[3] , Pix_L[3] , 0,1,0, 1,0);
	trans_Pix(Pix_D[3] , Im_W[3] , Im_H[3] , Pix_L[3] , 1,0,1);

	screen_grid(temp,Im_W[3],Im_H[3],50,50,0.5,1, 50,50,0.5,1);
	//screen_half(temp,Im_W[3],Im_H[3],100);

	//init_rec(temp , Im_W[3] , Im_H[3] , 20);//矩孔初始化
	FFT2(temp , Im_W[3] , Im_H[3]);//傅里叶变换
	fftshift(temp , Im_W[3] , Im_H[3]);//移动级次

	CP_m2i(temp , Pix_D[3] , Im_W[3] , Im_H[3] , Pix_L[3] , 1,1, 0,1,0, 1);//变换结果加至图像（取模，规范最大值225）
	free(temp);
	//******************************
    //初始化GLUT并运行
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);

	glutInitWindowPosition(0, 0);
	glutInitWindowSize(Im_W[0], Im_H[0]);
	glutCreateWindow("矩孔_FFT");
    glutDisplayFunc(&display_0);

	glutInitWindowPosition(60, 60);
	glutInitWindowSize(Im_W[1], Im_H[1]);
	glutCreateWindow("图片_FFT");
    glutDisplayFunc(&display_1);
	
	glutInitWindowPosition(120, 120);
	glutInitWindowSize(Im_W[2], Im_H[2]);
	glutCreateWindow("圆孔_FFT");
    glutDisplayFunc(&display_2);

	glutInitWindowPosition(120, 120);
	glutInitWindowSize(Im_W[3], Im_H[3]);
	glutCreateWindow("矩孔_FFT");
    glutDisplayFunc(&display_3);

    glutMainLoop();
    return 0;
}
